package com.csjbot.blackgaga.feature.settings;

import android.content.Context;
import android.database.ContentObserver;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.csjbot.blackgaga.R;
import com.csjbot.blackgaga.base.BaseFullScreenActivity;
import com.csjbot.blackgaga.base.BasePresenter;
import com.csjbot.blackgaga.global.Constants;
import com.csjbot.blackgaga.util.BlackgagaLogger;
import com.csjbot.blackgaga.util.SharedKey;
import com.csjbot.blackgaga.util.SharedPreUtil;
import com.csjbot.blackgaga.util.TonePlayer;
import com.csjbot.blackgaga.util.VolumeUtil;
import com.jaygoo.widget.RangeSeekBar;

import butterknife.BindView;

/**
 * Created by 孙秀艳 on 2017/10/20.
 * 系统设置  音量设置
 */

public class SettingsVolumeActivity extends BaseFullScreenActivity implements RangeSeekBar.OnRangeChangedListener{
    private int volume;
    private float volumeProcess;
    private AudioManager mAudioManager;
    private SettingsContentObserver mSettingsContentObserver;
    private int current;

    @BindView(R.id.volume_setting)
    RangeSeekBar volumeSetting;

    @Override
    public void afterViewCreated(Bundle savedInstanceState) {
        super.afterViewCreated(savedInstanceState);
    }

    @Override
    public void init() {
        super.init();
        getTitleView().setSettingsVisibility(View.GONE);
        getTitleView().setSettingsPageBackVisibility(View.VISIBLE);
        setupVolumn();
        volumeSetting.setOnRangeChangedListener(this);
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_settings_volume;
    }

    @Override
    public boolean isOpenChat() {
        return false;
    }

    @Override
    public boolean isOpenTitle() {
        return true;
    }

    private void setupVolumn() {
//        registerVolumeChangeReceiver();
        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        current = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        setRankSeekBarProgress(current);
    }

    private void registerVolumeChangeReceiver() {
        mSettingsContentObserver = new SettingsContentObserver(this, new Handler());
        getApplicationContext().getContentResolver().registerContentObserver(Settings.System.CONTENT_URI, true, mSettingsContentObserver);
    }

    private void unregisterVolumeChangeReceiver() {
        getApplicationContext().getContentResolver().unregisterContentObserver(mSettingsContentObserver);
    }

    public class SettingsContentObserver extends ContentObserver {
        Context context;
        public SettingsContentObserver(Context c, Handler handler) {
            super(handler);
            context = c;
        }
        @Override
        public boolean deliverSelfNotifications() {
            return super.deliverSelfNotifications();
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            BlackgagaLogger.debug("change currentVolume" + currentVolume);
            setRankSeekBarProgress(currentVolume);
        }
    }

    @Override
    public void onRangeChanged(RangeSeekBar view, float min, float max, boolean isFromUser) {
        if (isFromUser) {
            if ((int)min == 0) {
                volume = (int) 0.0f;
            } else if ((int)min == 10) {
                volume = (int) 1.6f;
            } else if ((int)min == 20) {
                volume = (int) 3.2f;
            } else if ((int)min == 30) {
                volume = (int) 4.8f;
            } else if ((int)min == 40) {
                volume = (int) 6.4f;
            } else if ((int)min == 50) {
                volume = (int) 8.0f;
            } else if ((int)min == 60) {
                volume = (int) 9.6f;
            } else if ((int)min == 70) {
                volume = (int) 11.2f;
            } else if ((int)min == 80) {
                volume = (int) 12.8f;
            } else if ((int)min == 90) {
                volume = (int) 14.4f;
            } else if ((int)min == 100) {
                volume = 16;
            }
            //改变系统的音量
            BlackgagaLogger.debug("set volume" + "max" + max + "min" + min + "volume" + volume);
            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
            SharedPreUtil.putInt(SharedKey.VOICENAME, SharedKey.VOICEKEY, volume);
        } else {
            if (max == 100) {
                TonePlayer.playUseSoundPool(this, Constants.VOLUME_PATH, 3);
            }
        }
    }

    /**
     * 刻度转换
     */
    private void setRankSeekBarProgress(int currentVolume) {
        if (currentVolume == 0) {
            volumeProcess = 0;
        } else if (currentVolume == 1) {
            volumeProcess = 10;
        } else if (currentVolume == 2) {
            volumeProcess = 10;
        } else if (currentVolume == 3) {
            volumeProcess = 20;
        } else if (currentVolume == 4) {
            volumeProcess = 30;
        } else if (currentVolume == 5) {
            volumeProcess = 30;
        } else if (currentVolume == 6) {
            volumeProcess = 40;
        } else if (currentVolume == 7) {
            volumeProcess = 40;
        } else if (currentVolume == 8) {
            volumeProcess = 50;
        } else if (currentVolume == 9) {
            volumeProcess = 60;
        } else if (currentVolume == 10) {
            volumeProcess = 60;
        } else if (currentVolume == 11) {
            volumeProcess = 70;
        } else if (currentVolume == 12) {
            volumeProcess = 80;
        } else if (currentVolume == 13) {
            volumeProcess = 80;
        } else if (currentVolume == 14) {
            volumeProcess = 90;
        } else if (currentVolume == 15) {
            volumeProcess = 100;
        }
        volumeSetting.setValue(volumeProcess);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                int volume = VolumeUtil.addMediaVolume(this);
                setRankSeekBarProgress(volume);
                return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                int cutVolume = VolumeUtil.cutMediaVolume(this);
                setRankSeekBarProgress(cutVolume);
                return true;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
//        unregisterVolumeChangeReceiver();
        super.onDestroy();
    }
}
